//
//  NearbyDetailViewController.m
//  CommonFrame
//
//  Created by Bang-guo on 15/7/22.
//  Copyright (c) 2015年 guobang. All rights reserved.
//

#import "NearbyDetailViewController.h"

@interface NearbyDetailViewController ()

@end

@implementation NearbyDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"collectionImage.png"] style:UIBarButtonItemStylePlain target:self action:@selector(rightButton:)];
    self.navigationItem.rightBarButtonItem = rightButton;
    
    self.nearbyDetailS = [[NearbyDetailScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 20 - 44 - 49)];
    self.nearbyDetailS.myDelegate = self;
    [self.view addSubview:self.nearbyDetailS];
    self.navigationItem.title = @"详情";
    
    [self getDataValue];
    //让菊花旋转起来
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"正在加载中...";
    [self.HUD show:YES];


    
}
-(void)rightButton:(UIBarButtonItem *)button {
    
    NSMutableArray *array = [NSMutableArray arrayWithArray:[[CoreDataHandle shareCoreData] selectAll:@"NearbyDetailCDM"]];
    NSMutableArray *placeIdArray = [NSMutableArray array];
    for (NearbyDetailCDM *near in array) {
        
        [placeIdArray addObject:[NSString stringWithFormat:@"%@", near.placeId]];
        
    }
    NSString *str = [NSString stringWithFormat:@"%@", self.nearbyDetail.placeId];

    if (![placeIdArray containsObject:str]) {
        
        [[CoreDataHandle shareCoreData] insert:@"NearbyDetailCDM" withPhoto:self.nearbyDetail.photo withName:self.nearbyDetail.name withRating:self.nearbyDetail.rating withRecommended_reason:self.nearbyDetail.recommended_reason withAddress:self.nearbyDetail.address withArrival_type:self.nearbyDetail.arrival_type withOpening_time:self.nearbyDetail.opening_time withTel:self.nearbyDetail.tel withPlaceId:self.nearbyDetail.placeId withDescriptionP:self.nearbyDetail.descriptionP withType:self.nearbyDetail.type withLat:self.nearbyDetail.lat withLng:self.nearbyDetail.lng];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"收藏成功!" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已经收藏过!" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        
        
    }
    

    
    
}

-(void)location:(NearbyDetail *)nearbyD {
    
    MapLocationViewController *map = [[MapLocationViewController alloc] init];
    map.nearbyDetail = nearbyD;
    [self.navigationController pushViewController:map animated:YES];
    
    
}

-(void)sendModle:(NearbyDetail *)nearbyD {
    
    NearbyPhotoViewController *nearbyPhoto = [[NearbyPhotoViewController alloc] init];
    nearbyPhoto.nearbyDetail = nearbyD;
    [self.navigationController pushViewController:nearbyPhoto animated:YES];
    
}

-(void)getDataValue {

    NSString *str_url = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/5/%@/", self.placeId];
    NSLog(@"=====  %@", str_url);
    [AFNGetDataValue getDataAFN:str_url block:^(NSMutableDictionary *dataDic) {

        NearbyDetail *nearbyD = [[NearbyDetail alloc] init];
        [nearbyD setValuesForKeysWithDictionary:dataDic];
        nearbyD.descriptionP = [dataDic objectForKey:@"description"];
        nearbyD.placeId = [dataDic objectForKey:@"id"];
        nearbyD.type = [dataDic objectForKey:@"type"];
        NSMutableArray *array = [NSMutableArray arrayWithArray:[dataDic objectForKey:@"hottest_places"]];
        if (array.count != 0) {
            
            nearbyD.photo = [array[0] objectForKey:@"photo"];
            
        }
        nearbyD.lat = [NSString stringWithFormat:@"%@", [[dataDic objectForKey:@"location"] objectForKey:@"lat"]];
        nearbyD.lng = [NSString stringWithFormat:@"%@", [[dataDic objectForKey:@"location"] objectForKey:@"lng"]];
        [self.nearbyDetailS createSubViews:nearbyD];
        self.nearbyDetail = nearbyD;
        [self.HUD hide:YES];
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
